package com.cloud.config;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @Author wangjian (jian.wang@bertelsmann.com.cn)
 * @Date 2016/11/15 10:29
 */
@Configuration
@EnableSwagger2
@EnableConfigurationProperties({SwaggerProperties.class})
public class SwaggerConfig {

    @Value("${swagger.ui.enable}") //该配置项在配置中心管理
    private boolean environmentSpecificBooleanFlag;

    @Autowired
    private SwaggerProperties swaggerProperties;

    public SwaggerConfig() {
    }

    @Bean
    public Docket createRestApi() {
        String basePackage = "com.cloud";
        if(StringUtils.isNotBlank(this.swaggerProperties.getBasePackage())) {
            basePackage = this.swaggerProperties.getBasePackage();
        }

        return (new Docket(DocumentationType.SWAGGER_2)).enable(environmentSpecificBooleanFlag).apiInfo(this.getApiInfo()).select().apis(RequestHandlerSelectors.basePackage(basePackage)).paths(PathSelectors.any()).build();
    }

    private ApiInfo getApiInfo() {
        String title = "CLOUD SOA RESTFUL APIs";
        if(StringUtils.isNotBlank(this.swaggerProperties.getTitle())) {
            title = this.swaggerProperties.getTitle();
        }

        String description = "通用项目接口文档";
        if(StringUtils.isNotBlank(this.swaggerProperties.getDescription())) {
            description = this.swaggerProperties.getDescription();
        }

        String termsOfServiceUtl = "http://www.cloud.cn";
        if(StringUtils.isNotBlank(this.swaggerProperties.getTermsOfServiceUrl())) {
            termsOfServiceUtl = this.swaggerProperties.getTermsOfServiceUrl();
        }

        String version = "1.0";
        if(StringUtils.isNotBlank(this.swaggerProperties.getVersion())) {
            version = this.swaggerProperties.getVersion();
        }

        String license = "";
        if(StringUtils.isNotBlank(this.swaggerProperties.getLicense())) {
            license = this.swaggerProperties.getLicense();
        }

        String licenseUrl = "";
        if(StringUtils.isNotBlank(this.swaggerProperties.getLicenseUrl())) {
            licenseUrl = this.swaggerProperties.getLicenseUrl();
        }

        return (new ApiInfoBuilder()).title(title).description(description).termsOfServiceUrl(termsOfServiceUtl).version(version).license(license).licenseUrl(licenseUrl).build();
    }
}
